package com.learnpilot.datechange.service.impl;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.learnpilot.datechange.service.CommandExecutor;


public class CommandExecutorImpl implements CommandExecutor {
	private final Logger LOG = LoggerFactory.getLogger(CommandExecutorImpl.class);

	public void runCommand(String[] command) {
		try {
			Runtime.getRuntime().exec(command);
			Process p = Runtime.getRuntime().exec(command);
			BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String line = reader.readLine();
			while (line != null) {
				LOG.debug(line);
				line = reader.readLine();
			}
			BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
			BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
			String error;
			String input;
			while ((error = stdError.readLine()) != null) {
				LOG.error("Could not execute "+command[2]+" command : "+error);
			}
			while ((input = stdInput.readLine()) != null) {
				LOG.debug(input);
			}

		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e.getMessage());
		}
	}
}
